global ERROR_TOL;
global EPOCHS;
global EXTERNALTESTING;
global EXTERNALWEIGHTS;

constants;

data = load('resources/patternsAndSolutions.txt');
textdata = {'x1', 'x2', 'z'};
colheaders = {'x1', 'x2','z'};

resources.data = data;
resources.textdata = textdata;
resources.colheaders = colheaders;

[patterns, solutions, testingPatterns, testingSolutions] = getTrainingData(resources.data); % Genero los patrones y las salidas deseadas
vectorWeights = generateWeights();                % Genero las matrices con los pesos peque??os aleatorios

if EXTERNALWEIGHTS==1
    load('resources/weights.mat', '-mat', 'wf');
else
    wf = trainNeuralNetwork (patterns, vectorWeights, solutions, ERROR_TOL, EPOCHS);    
end


disp('RESULTADO DEL ENTRENAMIENTO:');
for i = 1:length(solutions(1,:))
    disp('Pattern: ');
    disp(patterns(:,i)');
    output = valPattern(wf, patterns(:,i));
    disp('Output: ');
    disp(output);
    disp('Error:')
    disp(abs(solutions(i) - output));
end

% Devuelve los datos necesarios para hacer un surf de TODOS los patrones
[xint, yint, zint, zinterr] = surfData(patterns,solutions, wf);
surf(xint, yint, zint);
xlabel('x');
ylabel('y');
zlabel('z = f(x,y)');

if EXTERNALTESTING==1
    externalTestingPatterns = load('resources/testPatterns.txt');
    testingPatterns = [externalTestingPatterns(:,1,:)';externalTestingPatterns(:,2,:)'];
    testingSolutions = externalTestingPatterns(:,3,:)';
end

[xtest, ytest, ztest, ztesterr] = surfData(testingPatterns, testingSolutions, wf);
surf(xtest, ytest, ztest);
surf(xtest, ytest, ztesterr);
disp('TERMINATED');
